<?php


namespace app\api\controller\v1;


use app\common\controller\UserBaseController;
use app\api\model\ArticleModel;
use app\api\model\ArticleCateModel;
use app\common\exception\BaseException;

class ArticleController extends UserBaseController
{
    function cate_listAction()
    {
        $this->paramlist = [
          'visitable' => 'optint',
        ];

        $this->check_params();

        $this->resp['list'] = (new ArticleCateModel())->query_list($this->argv);

        return $this->ret_resp();
    }

    function addAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'cate_id' => 'int',
            'title' => 'string',
            'tags' => 'array',
            'content' => 'string',
            'city_id' => ['optstring', null],
            'rank' => 'int',
            'start_time' => 'string',
            'end_time' => 'string',
        );

        $this->check_params();
        $this->check_token();

        (new ArticleModel())->add($this->argv);

        return $this->ret_resp();
    }

    function updateAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'int',
            'cate_id' => 'int',
            'title' => 'string',
            'tags' => 'array',
            'content' => 'string',
            'start_time' => 'optstring',
            'end_time' => 'optstring',
        );

        $this->check_params();
        $this->check_token();

        (new ArticleModel())->update_article($this->argv['id'], $this->argv);

        return $this->ret_resp();
    }


    function delAction()
    {
        $this->paramlist = array(
            'token' => 'string',
            'id' => 'int',
        );


        $this->check_params();
        $this->check_token();

        $data = [
            'state' => -1,
        ];
        (new ArticleModel())->update_article($this->argv['id'], $data);

        return $this->ret_resp();
    }

    function article_list_by_tagAction()
    {
        $this->paramlist = array(
            'tag' => 'string',
            'coordinate' => ['optarray', null],
            'city_id' => ['optstring', null],
        );

        $this->check_params();

        $city_id = $this->argv['city_id'];


        $this->resp['list'] = (new ArticleModel())->query_list_by_tag($city_id, $this->argv['tag']);

        return $this->ret_resp();
    }


    function article_list_by_cateAction()
    {
        $this->paramlist = [
          'cate_id' => 'int',
          'city_id' =>  ['optstring',null]
        ];

        $this->check_params();

        $city_id = $this->argv['city_id'];
        $cate_id = $this->argv['cate_id'];

        if ($cate_id == 1) {
            $this->resp['list'] = (new ArticleModel())->query_list_by_tag($city_id, '热门问题');
        } else if ($cate_id == 0) {
            $this->resp['list'] = (new ArticleModel())->query_list($city_id);
        } else {
            $this->resp['list'] = (new ArticleModel())->query_list_by_cate($city_id, $this->argv['cate_id']);
        }

        return $this->ret_resp();
    }

    function protocal_contentAction()
    {
        $this->paramlist =[
          'name' => 'string'
        ];

        $this->check_params();

        $id = null;

        if ($this->argv['name'] == '充值协议'){
            $id = 43;
        } else if ($this->argv['name'] == '押金协议'){
            $id = 44;
        } else if ($this->argv['name'] == '用户协议'){
            $id = 45;
        } else if ($this->argv['name'] == '会员卡'){
            $id = 47;
        } else if ($this->argv['name'] == '多多运维'){
            $id = 48;
        }

        $this->resp['content'] = (new ArticleModel())->get_content($id);

        return $this->ret_resp();
    }

    function tagsAction()
    {
        $this->resp['tags'] = [
            '常见问题',
            '热门问题',
            '猜您想问',
            '新手引导',
        ];

        return $this->ret_resp();
    }
}